package dao;

import entity.Notice;
import utils.PropertiesUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class NoticeDao {
    //添加消息
    public void add(Notice n) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "insert into notice(e_id,n_text,n_time) values(?,?,?)";
        Object[] params = {n.getE_id(),n.getN_text(),n.getN_time()};
        queryRunner.update(sql, params);
    }

    //根据n_id删除消息
    public void delete(Integer n_id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "delete from notice where n_id = ?";
        queryRunner.update(sql, n_id);
    }

    //查询e_id为e_id的消息
    public List<Notice> listNotice(Integer e_id) throws SQLException{
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from notice where e_id = "+e_id;
        List<Notice> list = queryRunner.query(sql,new BeanListHandler<>(Notice.class));
        return list;
    }
}

